package com.xbongbong.paas.help.subform;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.help.subform.abstracts.AbstractTransferSubForm;
import com.xbongbong.saas.domain.entity.ContractProductEntity;
import com.xbongbong.saas.enums.subform.ContractProductSubFormEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 合同产品 es和数据库格式转化
 * @author 魏荣杰
 * @date 2020/8/24 17:24
 * @since
 * @version
 */
@Component
public class ContractProductTransferSubForm extends AbstractTransferSubForm {

    private static final Logger LOG = LoggerFactory.getLogger(ContractProductTransferSubForm.class);

    public PaasFormDataEntityExt transferEntityToFormData(ContractProductEntity contractProductEntity) {
        PaasFormDataEntityExt paasFormDataEntity = new PaasFormDataEntityExt();
        paasFormDataEntity.setId(contractProductEntity.getId());
        paasFormDataEntity.setFormId(contractProductEntity.getFormId());
        paasFormDataEntity.setCorpid(contractProductEntity.getCorpid());
        paasFormDataEntity.setDel(contractProductEntity.getDel());
        paasFormDataEntity.setAddTime(contractProductEntity.getAddTime());
        paasFormDataEntity.setUpdateTime(contractProductEntity.getUpdateTime());

        JSONObject dbJson = JSONObject.parseObject(JSON.toJSONString(contractProductEntity));

        JSONObject data = contractProductEntity.getData();
        List<String> removeKeys = Arrays.asList("estimateCost", "productSubId", "enableMultiUnit", "refProductId", "shelfLifeDays", "enableBatchShelfLife", "enableSerialNumber");
        removeKeys.forEach(data::remove);
        ContractProductSubFormEnum[] values = ContractProductSubFormEnum.values();
        for (ContractProductSubFormEnum value : values) {
            String dbAttr = value.getDbAttr();
            String dataAttr = value.getDataAttr();
            Integer fieldType = value.getFieldType();
            try {
                formatDataForEs(dbJson, data, dbAttr, dataAttr, fieldType);
            } catch (Exception e) {
                LOG.info("",e);
                LOG.info("==dbAttr====="+dbAttr+"---dataAttr-----"+dataAttr);
                LOG.info("======="+contractProductEntity.toString()+"--------"+data);
            }
        }
        paasFormDataEntity.setData(data);

        return paasFormDataEntity;
    }

    public List<PaasFormDataEntityExt> transferEntityToFormDataList(List<ContractProductEntity> contractProductEntities) {
        List<PaasFormDataEntityExt> paasFormDataEntities = new ArrayList<>();
        for (ContractProductEntity contractProductEntity : contractProductEntities) {
            PaasFormDataEntityExt paasFormDataEntity = transferEntityToFormData(contractProductEntity);
            paasFormDataEntities.add(paasFormDataEntity);
        }
        return paasFormDataEntities;
    }

    public ContractProductEntity transferFormDataToEntity(PaasFormDataEntity paasFormDataEntity) {

        JSONObject data = paasFormDataEntity.getData();

        JSONObject dbData = new JSONObject();
        ContractProductSubFormEnum[] contractProductSubFormEnums = ContractProductSubFormEnum.values();
        for (ContractProductSubFormEnum contractProductSubFormEnum : contractProductSubFormEnums) {
            String dbAttr = contractProductSubFormEnum.getDbAttr();
            String dataAttr = contractProductSubFormEnum.getDataAttr();
            Integer fieldType = contractProductSubFormEnum.getFieldType();
            formatDataForDb(data, dbData, dbAttr, dataAttr, fieldType);
            data.remove(dataAttr);
        }
        ContractProductEntity contractProductEntity = JSON.parseObject(dbData.toJSONString(), ContractProductEntity.class);
        contractProductEntity.setData(data);
        contractProductEntity.setAddTime(paasFormDataEntity.getAddTime());
        contractProductEntity.setUpdateTime(paasFormDataEntity.getUpdateTime());
        contractProductEntity.setCorpid(paasFormDataEntity.getCorpid());
        contractProductEntity.setDel(paasFormDataEntity.getDel());
        contractProductEntity.setId(paasFormDataEntity.getId());
        contractProductEntity.setFormId(paasFormDataEntity.getFormId());

        return contractProductEntity;
    }


    public List<ContractProductEntity> transferFormDataToEntityList(List<PaasFormDataEntity> paasFormDataEntities) {
        List<ContractProductEntity> contractProductEntities = new ArrayList<>();
        for (PaasFormDataEntity paasFormDataEntity : paasFormDataEntities) {
            ContractProductEntity contractProductEntity = transferFormDataToEntity(paasFormDataEntity);
            contractProductEntities.add(contractProductEntity);
        }
        return contractProductEntities;

    }

}
